var express=require("express")
var fs=require("fs")
var app=express()
app.listen("6500",function(){
	console.log("服务器已开启,端口号为6500")
})
//注册
var msgArr=[];
app.post("/receiveMsg",function(req,res){
	res.header('Access-Control-Allow-Origin','*')
	var param="";
	req.on("data",function(data){
		param+=data;
	})
	req.on("end",function(){
		console.log(JSON.parse(param))
		msgArr.push(JSON.parse(param))
		fs.writeFile("data.json",JSON.stringify(msgArr),"utf-8")
		res.send("完成")
	})
	
})
//登录
app.post("/denglu",function(req,res){
	res.header('Access-Control-Allow-Origin','*')
	var str=""
	req.on("data",function(data){
		str+=data
	})
	req.on("end",function(){
		var obj=JSON.parse(str);
		fs.readFile("data.json","utf-8",function(err,data){
			if (err) {
				console.log(err)
			} else{
				var arr=JSON.parse(data)
				var isend=false
				for (var i=0;i<arr.length;i++) {
					if (obj.email==arr[i].email&&obj.pwd==arr[i].pwd) {
						issend=true
					} 
				}
				if (issend) {
					res.send("1")
				} else{
					res.send("0")
				}
			}
		})
	})
})
app.get("/dianpu",function(req,res){
	res.header('Access-Control-Allow-Origin','*')
	console.log(req.query.pageNum)
//	res.send(req.query.pageNum)
	res.sendFile(__dirname+"/data/shop"+req.query.pageNum+".json")
})
app.all("*",function(req,res){
	res.header("Access-Control-Allow-Origin","*")
	res.sendFile(__dirname+req.url)
})
